
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

// import com.newupbank.bgc.basic.util.json.BasicJsonUtil;
import com.newupbank.xmlparser.XmlParser;
import com.newupbank.xmlparser.XmlParserConfig;
import org.dom4j.tree.DefaultAttribute;
import jodd.io.FileUtil;
import jodd.util.StringUtil;

/**
 *
 * @author lifanhong
 */
public class NewEmptyJUnitTest2 {

	// public static final BasicJsonUtil BJU = BasicJsonUtil.getInstance();

	public void rhzxResolveRows2(String oracelDbName, String toHivetable, String hiveSourceTable, String getConfigSql,
			String hiveDbName, String para_date) throws Exception {
		List<HashMap<String, String>> configMapList = new ArrayList<>();

		StringBuffer sb = new StringBuffer();
		sb.append("from " + hiveSourceTable + " where " + "  ods_update_date='" + para_date + " ') "
				+ " t lateral view src_es.jxxmlv2( t.xmlreport  , ");

		for (HashMap<String, String> map : configMapList) {
			if (map.get("sourcefield").startsWith("/")) {
				XmlParserConfig xpc = new XmlParserConfig(map.get("sourcefield"), map.get("targetfield"),
						map.get("rtype"));
				sb.append("'" +
						// BJU.toJsonString(xpc)
						JSON.toJSONString(xpc)
						+ "'" + ",");
			}
		}
		sb.append("'row_num()'");
		sb.append(")");
		String coumlum = sb.toString().substring(0, sb.toString().length());
		String sql = " insert overwrite table " + toHivetable + " partition (ods_update_date='" + para_date + "')"
				+ "  select " + coumlum + " from " + hiveSourceTable + " t where  t.ods_update_date='" + para_date
				+ "'";
	}
	public static void main2(String[] args) throws Exception {

		XmlParser mxp = new XmlParser();
		// mxp.getCommonNode(configList);
		// XmlParserConfig xpc0 = new
		// XmlParserConfig("/ReportMessage/Header/MessageHeader/ReportSN", "c2", "1N");
		// XmlParserConfig xpc1 = new
		// XmlParserConfig("/ReportMessage/Announce/DissentInfo/Content", "c2", "N");
		// XmlParserConfig xpc2 = new
		// XmlParserConfig("/ReportMessage/Announce/DissentInfo/GetTime", "c2", "N");

		XmlParserConfig xpc0 = new XmlParserConfig("/ReportMessage/Header/MessageHeader/ReportSN", "c1", "1N");
		XmlParserConfig xpc1 = new XmlParserConfig(
				"/ReportMessage/CreditDetail/Loan/SpecialTrade/ChangingAmount"
		 , "c2", "N");
		XmlParserConfig xpc2 = new XmlParserConfig(
				"/ReportMessage/CreditDetail/Loan/SpecialTrade/GetTime", "c3", "N");
		XmlParserConfig xpc3 = new XmlParserConfig(
				"/ReportMessage/CreditDetail/Loan/SpecialTrade/Content", "c4", "N");

		List<XmlParserConfig> configList = new ArrayList<>(2);
		configList.add(xpc0);
		configList.add(xpc1);
		configList.add(xpc2);
		configList.add(xpc3);
		String fsa = mxp.getCommonNode(configList);
		if (fsa.endsWith("/")) {
			fsa = fsa.substring(0, fsa.length() - 1);
		}
		System.out.println("fsa=" + fsa);

		// MulXmlParser1 mulXmlParser = new MulXmlParser1();
		Object[] objects = new Object[7];
		objects[0] = FileUtil.readString("src/test/Test.XmlParser");
		String strXml = Objects.toString(objects[0]);
		// /ReportMessage/CreditDetail/Loan/Cue
		SAXReader reader = new SAXReader();
		StringReader stringReader = new StringReader(strXml);
		org.dom4j.Document document = reader.read(stringReader);
		// 父节点
		List<Element> loanList = document.selectNodes(fsa);
		for (Element node : loanList) {
			System.out.println("node=" + node);
			System.out.println("node.asXML()=" + node.asXML());
			int c=1;
			for (XmlParserConfig xpc : configList) {
				System.out.println(c++);
				if ("1N".equals(xpc.getType())) {
					continue;
				}
				String src = xpc.getSrc();
				System.out.println("src=" + src);
				String xs = src.substring(fsa.length() + 1);
				System.out.println("xs=" + xs);
				Node rnode = node.selectSingleNode(xs);
				System.out.println("rnode=" + ( rnode==null ? rnode :rnode.asXML() ));
			}
			// System.out.println("node.asXML()=" + node.asXML());
			// 子节点
			/*
			 * Node rnode = node.selectSingleNode("Cue"); System.out.println("node.asXML()="
			 * + node.getDocument().selectNodes(xpathExpression)); if (rnode != null) {
			 * String neirong = rnode.getText(); neirong = StringUtil.trimLeft(neirong);
			 * neirong = StringUtil.trimRight(neirong); System.out.println("rnode=" +
			 * neirong); } else { System.out.println("rnode=" + rnode); }
			 *
			 */
		}
		// objects[1]="/ReportMessage/Header/MessageHeader/ReportSN";
		// objects[2]="/ReportMessage/PublicInfo/AdminAward/Organname";
		// objects[3]="/ReportMessage/PublicInfo/AdminAward/Content";
		// objects[4]="/ReportMessage/PublicInfo/AdminAward/BeginDate";
		// objects[5]="/ReportMessage/PublicInfo/AdminAward/EndDate";
		// objects[6]="row_num()";
		// System.out.println(objects[0]);
		// mulXmlParser.process(objects);
	}

	/**
	 * 单个属性的时候，报错，使用判断，没问题了
	 * 多行，如果以@结束，去掉@
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		XmlParser mxp = new XmlParser(); 
		XmlParserConfig xpc0 = new XmlParserConfig("/Application/@AppId", 
				"c1", "1N");
		XmlParserConfig xpc1 = new XmlParserConfig(
				"/Application/ScoreModelReturnInfo/@AssociationID"
		 , "c2", "N");
		XmlParserConfig xpc2 = new XmlParserConfig(
				"/Application/ScoreModelReturnInfo/ScoredCharacteristic/@CharacteristicName"
		 , "c3", "N");
		XmlParserConfig xpc3 = new XmlParserConfig(
				"/Application/InternalAppInfoVariable/@cityAvgWage"
		 , "c4", "N");
		List<XmlParserConfig> configList = new ArrayList<>(2);
		configList.add(xpc0);
		configList.add(xpc1);
		configList.add(xpc2);
//		configList.add(xpc3);
		String fsa = mxp.getCommonNode(configList);
		if (fsa.endsWith("@")) {
			fsa = fsa.substring(0, fsa.length() - 1);
		}
		if (fsa.endsWith("/")) {
			fsa = fsa.substring(0, fsa.length() - 1);
		}
		System.out.println("fsa=" + fsa);

		// MulXmlParser1 mulXmlParser = new MulXmlParser1();
		Object[] objects = new Object[7];
		objects[0] = FileUtil.readString("src/test/Test-xiaoxiang.xml");
		String strXml = Objects.toString(objects[0]);
		// /ReportMessage/CreditDetail/Loan/Cue
		SAXReader reader = new SAXReader();
		StringReader stringReader = new StringReader(strXml);
		org.dom4j.Document document = reader.read(stringReader);
		// 父节点
		List<Node>  dataList = document.selectNodes(fsa);
		System.out.println("dataList.size()=" + dataList.size());
		for (Node node : dataList) { 
			System.out.println("node=" + node);
			System.out.println("node.asXML()=" + node.asXML());
			int c=1;
			for (XmlParserConfig xpc : configList) {
				System.out.println(c++);
				if ("1N".equals(xpc.getType())) {
					continue;
				}
				String src = xpc.getSrc();
				System.out.println("src=" + src);
				String xs = null ;
				if(src.length() <= fsa.length()) {xs=src;}
				else {
					xs = src.substring(fsa.length() + 1);
				}
				System.out.println("xs=" + xs);
				Node rnode = node.selectSingleNode(xs);
				
				System.out.println("rnode.asXML()=" + ( rnode==null ? rnode :rnode.asXML() ));
				System.out.println("rnode.getText()=" + ( rnode==null ? rnode :rnode.getText() ));
			} 
			 
		} 
	}


}
